package com.mdd.admin.service.system.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mdd.admin.service.system.ISystemDevPayService;
import com.mdd.admin.validate.system.SystemDevPayParam;
import com.mdd.admin.validate.system.SystemWxPayConfigParam;
import com.mdd.admin.vo.system.SystemDevPayDetailVo;
import com.mdd.admin.vo.system.SystemDevWxPayConfig;
import com.mdd.admin.vo.system.SystemDevPayListVo;
import com.mdd.admin.vo.system.SystemDevWxPayDetailVo;
import com.mdd.common.entity.system.SystemDevPay;
import com.mdd.common.entity.system.SystemDevPayWay;
import com.mdd.common.mapper.system.SystemDevPayMapper;
import com.mdd.common.utils.StringUtil;
import com.mdd.common.utils.UrlUtil;
import com.mdd.common.utils.YmlUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 支付配置实现类
 * @author panweiliang
 */
@Service
public class SystemDevPayServiceImpl implements ISystemDevPayService {
        
    @Resource
    SystemDevPayMapper systemDevPayMapper;

    /**
     * 支付配置列表
     *
     * @author panweiliang
     * @return List<SystemDevPayListVo>
     */
    @Override
    public List<SystemDevPayListVo> list() {
        QueryWrapper<SystemDevPay> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc(Arrays.asList("sort", "id"));
        List<SystemDevPay> devPayList = systemDevPayMapper.selectList(queryWrapper);
        List<SystemDevPayListVo> list = new LinkedList<>();
        for(SystemDevPay item : devPayList) {
            SystemDevPayListVo vo = new SystemDevPayListVo();
            BeanUtils.copyProperties(item, vo);
            vo.setImage(UrlUtil.toAbsoluteUrl(item.getImage()));
            list.add(vo);
        }
        return list;
    }

    /**
     * 支付配置详情
     *
     * @author panweiliang
     * @param id 主键参数
     * @return SystemDevPay
     */
    @Override
    public SystemDevWxPayDetailVo getWxPayConfigDetail(Integer id) {
        SystemDevPay model = systemDevPayMapper.selectOne(
                new QueryWrapper<SystemDevPay>()
                    .eq("id", id)
                    .last("limit 1"));
        Assert.notNull(model, "数据不存在");
        SystemDevWxPayDetailVo vo = new SystemDevWxPayDetailVo();
        BeanUtils.copyProperties(model, vo);
        String configJson = model.getConfig();
        if (StringUtil.isNotEmpty(configJson)) {
            JSONObject configJsonObj = JSONObject.parseObject(configJson);
            SystemDevWxPayConfig systemDevPayConfig = configJsonObj.toJavaObject(SystemDevWxPayConfig.class);
            if ("demo".equals(YmlUtil.get("like.runtime.mode"))) {
                vo.setInterface_version(systemDevPayConfig.getInterface_version());
                vo.setMerchant_type(systemDevPayConfig.getMerchant_type());
                vo.setMch_id(systemDevPayConfig.getMch_id().replaceAll("(?s).", "*"));
                vo.setPay_sign_key(systemDevPayConfig.getPay_sign_key().replaceAll("(?s).", "*"));
                vo.setApiclient_cert(systemDevPayConfig.getApiclient_cert().replaceAll("(?s).", "*"));
                vo.setApiclient_key(systemDevPayConfig.getApiclient_key().replaceAll("(?s).", "*"));
            } else {
                vo.setInterface_version(systemDevPayConfig.getInterface_version());
                vo.setMerchant_type(systemDevPayConfig.getMerchant_type());
                vo.setMch_id(systemDevPayConfig.getMch_id());
                vo.setPay_sign_key(systemDevPayConfig.getPay_sign_key());
                vo.setApiclient_cert(systemDevPayConfig.getApiclient_cert());
                vo.setApiclient_key(systemDevPayConfig.getApiclient_key());
            }

        }
        vo.setImage(UrlUtil.toAbsoluteUrl(model.getImage()));
        return vo;
    }


    /**
     * 支付配置
     *
     * @author panweiliang
     * @param id 主键参数
     * @return SystemDevPay
     */
    @Override
    public SystemDevWxPayConfig getWxPayConfig(Integer id) {
        SystemDevPay model = systemDevPayMapper.selectOne(
                new QueryWrapper<SystemDevPay>()
                        .eq("id", id)
                        .last("limit 1"));
        Assert.notNull(model, "数据不存在");
        SystemDevWxPayDetailVo vo = new SystemDevWxPayDetailVo();
        BeanUtils.copyProperties(model, vo);
        String configJson = model.getConfig();
        if (StringUtil.isNotEmpty(configJson)) {
            JSONObject configJsonObj = JSONObject.parseObject(configJson);
            return configJsonObj.toJavaObject(SystemDevWxPayConfig.class);
        }
        return null;
    }

    /**
     * 支付配置新增
     *
     * @author panweiliang
     * @param systemWxPayConfigParam 参数
     */
    @Override
    public void updateWxPayConfig(SystemWxPayConfigParam systemWxPayConfigParam) {
        Integer id = systemWxPayConfigParam.getId();
        if (id != null) {
            SystemDevPay model = systemDevPayMapper.selectOne(
                    new QueryWrapper<SystemDevPay>()
                            .eq("id", id)
                            .last("limit 1"));
            Assert.notNull(model, "数据不存在");
            model.setName(systemWxPayConfigParam.getName());
            model.setPayWay(systemWxPayConfigParam.getPayWay());
            SystemDevWxPayConfig config = new SystemDevWxPayConfig();
            BeanUtils.copyProperties(systemWxPayConfigParam, config);
            model.setConfig(JSONObject.toJSONString(config));
            model.setImage(UrlUtil.toRelativeUrl(systemWxPayConfigParam.getImage()));
            model.setSort(systemWxPayConfigParam.getSort());
            systemDevPayMapper.updateById(model);
        } else {
            SystemDevPay model = new SystemDevPay();
            model.setName(systemWxPayConfigParam.getName());
            model.setPayWay(systemWxPayConfigParam.getPayWay());
            SystemDevWxPayConfig config = new SystemDevWxPayConfig();
            BeanUtils.copyProperties(systemWxPayConfigParam, config);
            model.setConfig(JSONObject.toJSONString(config));
            model.setImage(UrlUtil.toRelativeUrl(systemWxPayConfigParam.getImage()));
            model.setSort(systemWxPayConfigParam.getSort());
            systemDevPayMapper.insert(model);
        }
    }

    @Override
    public SystemDevPayDetailVo getBalanceDetail(Integer id) {
        SystemDevPay model = this.systemDevPayMapper.selectById(id);
        Assert.notNull(model, "数据不存在");
        SystemDevPayDetailVo vo = new SystemDevPayDetailVo();
        BeanUtils.copyProperties(model, vo);
        vo.setImage(UrlUtil.toAbsoluteUrl(model.getImage()));
        return vo;
    }

    @Override
    public void updateBalance(SystemDevPayParam systemDevPayParam) {
        SystemDevPay model = this.systemDevPayMapper.selectById(systemDevPayParam.getId());
        Assert.notNull(model, "数据不存在");
        model.setPayWay(systemDevPayParam.getPayWay());
        model.setImage(UrlUtil.toRelativeUrl(systemDevPayParam.getImage()));
        model.setName(systemDevPayParam.getName());
        model.setSort(systemDevPayParam.getSort());
        this.systemDevPayMapper.updateById(model);

    }
}
